package com.peopledelete.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peopledelete.pojo.Origindayrecords;
import com.peopledelete.mapper.OrigindayrecordsMapper;
import com.peopledelete.service.OrigindayrecordsService;

/**
 * @author cyt
 * @create 2020-04-26 17:59
 */
@Service
public class OrigindayrecordsServiceImpl extends ServiceImpl<OrigindayrecordsMapper, Origindayrecords> implements OrigindayrecordsService {

    @Autowired
    OrigindayrecordsMapper mapper;

    @Autowired
    OrigindayrecordsService service;

    @Override
    public List<Map<String, Object>> getDayOutInNum(int hour) {
        // 先查出来当天的记录数
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        QueryWrapper<Origindayrecords> wrapper = new QueryWrapper<>();
        wrapper.eq("day_date", dateString);
        wrapper.select("sum(out_num) as OutNum,sum(in_num) as InNum");
        wrapper.eq("hour", hour);
        List<Map<String, Object>> maps = mapper.selectMaps(wrapper);
        return maps;
    }

    @Override
    public void insertDate(Integer inNum, Integer outNum) {

        Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
        int hour = c.get(Calendar.HOUR_OF_DAY);

        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);

        java.util.Date utilDate = new Date();                                    //获取java.util.Date对象---也即当前时间
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());    //将java.util.Date类型转换成java.sal.Date类型


        System.out.println(sqlDate);

        Origindayrecords records = new Origindayrecords();
        records.setDay_date(utilDate);
        records.setHour(hour);
        records.setOutNum(outNum);
        records.setInNum(inNum);
        mapper.insert(records);
    }


    // 查询出当天的累计出站、入站人数
    @Override
    public List<Map<String, Object>> getCntNum() {
        // 先查出来当天的记录数
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        QueryWrapper<Origindayrecords> wrapper = new QueryWrapper<>();
        wrapper.eq("day_date", dateString);
        wrapper.select("sum(out_num) as OutNum,sum(in_num) as InNum");
        List<Map<String, Object>> maps = mapper.selectMaps(wrapper);
        return maps;
    }

    // 清洗 每日原始记录到 每日记录表中
    @Override
    public int initRecords(String dateString, Integer begin, Integer end) {
        // 按照时间段求和
        int records = mapper.initRecords(dateString, begin, end);
        // 插入数据
        return records;
    }


}
